﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WechatDAL;
using WechatModel;
using System.Xml;
using NS.Utilities;

namespace BLL
{
    public class Wechat
    {
        /// <summary>
        /// 公众号消息解密
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public XmlDocument DecPublic(string signature, string timestamp, string nonce, string data)
        {
            EncryptDal dal = new EncryptDal();
            string result = "";
            if (dal.DecPublic(signature, timestamp, nonce, data, ref result) == 0)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(result);
                return doc;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 公众号接入时的认证
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <returns></returns>
        public static bool CheckInPublic(string signature, string timestamp, string nonce, out string x)
        {
            x = "";
            string token = WechatAPI4Public.Token;
            List<string> arr = new List<string> { token, timestamp, nonce };
            arr.Sort();
            string str = string.Join("", arr.ToArray());
            //SHA1加密后的字符串是大写，而腾讯传过来的比较字符串是小写，这点需要注意。
            str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower();
            x = str;
            return str == signature;
        }

        /// <summary>
        /// 企业号接入时的认证
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <param name="echoStr"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        public static bool CheckInEnt(string signature, string timestamp, string nonce, string echoStr, out string x)
        {
            x = "";
            EncryptDal edal = new EncryptDal();
            int i = edal.EntCheckIn(signature, timestamp, nonce, echoStr, ref x);
            return i == 0;
        }

        /// <summary>
        /// 获取公众号基本网页授权
        /// </summary>
        /// <param name="url">回调地址，授权后跳转到的页面</param>
        /// <returns></returns>
        public static string GetOAuthBasePublic(string url)
        {
            return W_OAuthDal.GetOAuthAddress(0, url, "snsapi_base", "");
        }

        /// <summary>
        /// 获取公众号基本网页授权
        /// </summary>
        /// <param name="url">回调地址，授权后跳转到的页面</param>
        /// <returns></returns>
        public static string GetOAuthBasePublic(string url, string state)
        {
            return W_OAuthDal.GetOAuthAddress(0, url, "snsapi_base", state);
        }

        /// <summary>
        /// 获取企业号基本网页授权
        /// </summary>
        /// <param name="url">回调地址，授权后跳转到的页面</param>
        /// <returns></returns>
        public static string GetOAuthBaseEnt(string url)
        {
            return W_OAuthDal.GetOAuthAddress(1, url, "snsapi_base", "");
        }

        /// <summary>
        /// 获取企业号基本网页授权
        /// </summary>
        /// <param name="url">回调地址，授权后跳转到的页面</param>
        /// <returns></returns>
        public static string GetOAuthBaseEnt(string url, string state)
        {
            return W_OAuthDal.GetOAuthAddress(1, url, "snsapi_base", state);
        }

        /// <summary>
        /// 获取用户Openid
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetOpenid(string code)
        {
            W_OAuthDal wdal = new W_OAuthDal();
            W_OAuthInfo m = wdal.GetOAuthToken(code);
            if (m == null)
                return "";
            else
                return m.openid;
        }

        /// <summary>
        /// 获取网页授权Token
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public W_OAuthInfo GetOAuthToken(string code)
        {
            W_OAuthDal wdal = new W_OAuthDal();
            return wdal.GetOAuthToken(code);
        }

        /// <summary>
        /// 企业号获取网页用户ID
        /// </summary>
        /// <param name="code"></param>
        /// <param name="aid"></param>
        /// <returns></returns>
        public string GetEntUserId(string code, int aid)
        {
            W_OAuthDal wdal = new W_OAuthDal();
            return wdal.GetOAuthUserinfoEnt(code, aid);
        }

        /// <summary>
        /// 计算公众号JS接口签名
        /// </summary>
        /// <param name="url">页面url</param>
        /// <param name="appid"></param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="noncestr">随机字符串</param>
        /// <returns></returns>
        public string GetJSSignPublic(string url, out string appid, out string timestamp, out string noncestr)
        {
            JSTickenDal jtdal = new JSTickenDal();
            return jtdal.GetJSSign(0, url, out appid, out timestamp, out noncestr);
        }

        /// <summary>
        /// 计算企业号JS接口签名
        /// </summary>
        /// <param name="url">页面url</param>
        /// <param name="appid"></param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="noncestr">随机字符串</param>
        /// <returns></returns>
        public string GetJSSignEnt(string url, out string appid, out string timestamp, out string noncestr)
        {
            JSTickenDal jtdal = new JSTickenDal();
            return jtdal.GetJSSign(1, url, out appid, out timestamp, out noncestr);
        }

        /// <summary>
        /// 为公众号创建自定义菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public bool BuildMenu(List<W_MenuInfo> menu)
        {
            W_MenuDal wdal = new W_MenuDal();
            string data = JsonHelper.T2JSON(menu);
            data = "{\"button\":" + data + "}";
            return wdal.Insert(data);
        }

        /// <summary>
        /// 删除公众号的自定义菜单
        /// </summary>
        /// <returns></returns>
        public bool DeleteMenu()
        {
            W_MenuDal wdal = new W_MenuDal();
            return wdal.Delete();
        }
    }
}
